﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System
Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Linq
Imports System.Xml.Serialization

Namespace roncliProductions.LibWowArmory.Character.Search

    ''' <summary>
    ''' A class to search characters on the Armory with
    ''' </summary>
    ''' <remarks></remarks>
    Public Class CharacterSearch
        Inherits WowArmoryData

        Private cssPage As New Schema.page

        ''' <summary>
        ''' The search text to search for characters on the Armory with
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property SearchText As String

        ''' <summary>
        ''' The URL that will be used to search for characters on the Armory with
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL As String
            Get
                Dim ubURL As New URLQueryStringCollection("http://www.wowarmory.com/search.xml")

                ubURL.Add("searchType", "characters")
                ubURL.Add("searchQuery", SearchText)

                Return ubURL.ToString
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request does not require login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Returns a list of search characters
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Characters As Collection(Of Character)
            Get
                If cssPage Is Nothing Then
                    Return New Collection(Of Character)
                End If
                If cssPage.armorySearch(0).searchResults(0).characters(0).character Is Nothing Then
                    Return New Collection(Of Character)
                End If
                Return (From a In cssPage.armorySearch(0).searchResults(0).characters(0).character Select New Character(a.battleGroup, a.battleGroupId, CType(a.classId, Enums.ClassType), CType(a.factionId, Enums.FactionType), CType(a.genderId, Enums.GenderType), a.guild, a.guildId, CDate(a.lastLoginDate), a.level, a.name, CType(a.raceId, Enums.RaceType), a.realm, a.relevance, a.searchRank, a.url)).ToCollection
            End Get
        End Property

        ''' <summary>
        ''' A default constructor
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
        End Sub

        ''' <summary>
        ''' A constructor that indicates the name of the person to search for, and performs the search
        ''' </summary>
        ''' <param name="strSearchText"></param>
        ''' <remarks></remarks>
        Public Sub New(ByVal strSearchText As String)
            SearchText = strSearchText
            Search()
        End Sub

        ''' <summary>
        ''' Performs the search on the armory
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub Search()
            MyBase.Retrieve()

            Dim xsSerializer As New XmlSerializer(cssPage.GetType)

            Using srXML As New StringReader(MyBase.Data)
                cssPage = CType(xsSerializer.Deserialize(srXML), Schema.page)
            End Using
        End Sub

    End Class

End Namespace